package com.project.nhantvt.MBean;



import java.util.List;


import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;

import org.primefaces.event.RowEditEvent;
import org.primefaces.event.SelectEvent;



import com.project.entity.Account;
import com.project.nhantvt.SessionBean.UserBeanRemote;
import com.project.solomon.SessionBean.PropertyBeanRemote;

@ManagedBean(name = "mUser")
@SessionScoped
public class MUser {

	private Account acc;
	
	private List <Account> allAccount;
	
	private int currentAccID;
	
	public MUser() {
		acc = new Account();
	}

	public int getCurrentAccID() {
		return currentAccID;
	}

	public void setCurrentAccID(int currentAccID) {
		this.currentAccID = currentAccID;
	}

	public Account getAcc() {
		return acc;
	}

	public void setAcc(Account acc) {
		this.acc = acc;
	}

	public void setAllAccount(List <Account> allAccount) {
		this.allAccount = allAccount;
	}

	public List <Account> getAllAccount() throws Exception{
		if (allAccount == null) {
			Context ctx = new InitialContext();
			UserBeanRemote accRe = (UserBeanRemote) ctx
					.lookup("MySweetHome/UserBean/remote");

			allAccount = accRe.GetAllAccount();
			if (allAccount.size() != 0) {
				return allAccount;
			} else
				return null;
		} else {
			return this.allAccount;
		}
	}
	
	public void update() throws Exception {
		InitialContext ctx = new InitialContext();
		UserBeanRemote accRe = (UserBeanRemote) ctx
				.lookup("MySweetHome/UserBean/remote");

		FacesContext context = FacesContext.getCurrentInstance();

		boolean check = accRe.UpdateUser(getAcc());

		if (check == false)
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_ERROR, "Error", "Update failed"));
	}
	
	public void updateRow(RowEditEvent evt) throws Exception {
		InitialContext ctx = new InitialContext();
		UserBeanRemote accRe = (UserBeanRemote) ctx
				.lookup("MySweetHome/UserBean/remote");
		Account rowAcc = (Account) evt.getObject();
		
		FacesContext context = FacesContext.getCurrentInstance();

		boolean check = accRe.UpdateUser(rowAcc);

		if (check)
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_INFO, "Successful",
					"Update Sucessful!"));
		else
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_ERROR, "Error", "Update failed"));
	}
	
	public String verifyToString(boolean isVerified) {
		if (isVerified==true)
			return "Verified";
		else 
			return "Unverified";
	}

	public String validStatus(boolean status) {
		if (status==true) 
			return "Activated";
		else
			return "Deactivated";
	}
	
	public String getPath(int accID) throws Exception {
		Context ctx = new InitialContext();
		UserBeanRemote accRe = (UserBeanRemote) ctx
				.lookup("MySweetHome/UserBean/remote");
		String path = accRe.getRandomIMGPath(accID);
		return path;
	}
}
